//
//  ViewController.swift
//  WQEmojiKeyboard-swift
//
//  Created by Xiang on 2021/5/21.
//

import UIKit

class ViewController: UIViewController, WQEmojiKeyBoardDelegate {
    
    private lazy var textView : UITextView = UITextView()
    
    private lazy var bottomToolView : UIView = UIView()
    
    private lazy var swBtn : UIButton = UIButton()
    
    private var isShowEmojiKB : Bool = false
    
    private var keyBoardY : CGFloat = 0
    
    private var emojiKeyBoard : WQEmojiKeyBoard?
    
    private var location : Int = 0
    
    private lazy var changeStringBtn : UIButton = UIButton()
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillChangeFrame(node:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }
    
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = kRGB(r: 246, g: 246, b: 246, a: 1)
    
        view.addSubview(textView)
        textView.frame = CGRect.init(x: 10, y: 88, width: kScreenW-20, height: 150)
        textView.font = UIFont.systemFont(ofSize: 16)
        textView.layer.borderWidth = 1
        textView.layer.borderColor = kRGB(r: 230, g: 230, b: 230, a: 1).cgColor
        textView.layer.cornerRadius = 5
        
        
        view.addSubview(bottomToolView)
        bottomToolView.frame = CGRect.init(x: 0, y: kScreenH, width: kScreenW, height: 50)
        bottomToolView.backgroundColor = kRGB(r: 230, g: 230, b: 230, a: 1)
        
        bottomToolView.addSubview(swBtn)
        swBtn.setTitle("切换为表情", for: .normal)
        swBtn.backgroundColor = UIColor.red
        swBtn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        swBtn.frame = CGRect.init(x: 10, y: 10, width: 30, height: 30)
        swBtn.sizeToFit()
        swBtn.addTarget(self, action: #selector(keyBoardSwitchBtnClick), for: .touchUpInside)
        
        
        emojiKeyBoard = WQEmojiKeyBoard.init(frame: CGRect.init(x: 0, y: kScreenH, width: kScreenW, height: 300), textView: self.textView)
        view.addSubview(emojiKeyBoard!)
        emojiKeyBoard!.delegate = self
        
        
        view.addSubview(changeStringBtn)
        changeStringBtn.setTitle("转换", for: .normal)
        changeStringBtn.frame = CGRect.init(x: 10, y: textView.frame.origin.y+textView.frame.height+15, width: 60, height: 30)
        changeStringBtn.backgroundColor = UIColor.red
        changeStringBtn.addTarget(self, action: #selector(changeStringBtnClick), for: .touchUpInside)
        
        
        
    }

    
    @objc func keyboardWillChangeFrame(node : Notification){
        // 1.获取动画执行的时间
        let duration = node.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
        // 2.获取键盘最终 Y值
        let endFrame = (node.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let y = endFrame.origin.y
        
        if y >= kScreenH || y == 0 {
            //键盘收起
            if self.isShowEmojiKB == true {
                keyBoardY = bottomToolView.frame.origin.y+bottomToolView.frame.height
            }else{
                self.invalidInputStatusWithAnimateDuration(duration: duration, boardY: y)
                keyBoardY = y
            }
        }else{
            //键盘弹起
            self.effectInputStatusWithAnimateDuration(duration: duration, boardY: y)
            keyBoardY = y
        }
        
    }
    
    @objc func keyBoardSwitchBtnClick() {
        if self.isShowEmojiKB == true {
            //切换为文字键盘
            swBtn.setTitle("切换为表情", for: .normal)
            isShowEmojiKB = false
            textView.becomeFirstResponder()
        }else{
            //切换为表情键盘
            swBtn.setTitle("切换为文字", for: .normal)
            isShowEmojiKB = true
            textView.resignFirstResponder()
        }
    }
    
    
    func effectInputStatusWithAnimateDuration(duration : Double,boardY:CGFloat) -> Void {
        //弹起
        UIView.animate(withDuration: duration) {
            self.bottomToolView.frame = CGRect.init(x: 0, y: boardY-50, width: kScreenW, height: 50)
            self.emojiKeyBoard!.frame = CGRect.init(x: 0, y: boardY, width: kScreenW, height: kScreenH-boardY)
            self.emojiKeyBoard!.keyBoardFrameChaneWithFrame(frame: self.emojiKeyBoard!.frame)
        } completion: { (finished) in
            self.isShowEmojiKB = false
        }

        
    }
    
    
    func invalidInputStatusWithAnimateDuration(duration : Double,boardY:CGFloat) -> Void {
        //收起
        UIView.animate(withDuration: duration) {
            self.bottomToolView.frame = CGRect.init(x: 0, y: kScreenH, width: kScreenW, height: 50)
            self.emojiKeyBoard!.frame = CGRect.init(x: 0, y: boardY, width: kScreenW, height: kScreenH-boardY)
            self.emojiKeyBoard!.keyBoardFrameChaneWithFrame(frame: self.emojiKeyBoard!.frame)
        } completion: { (finished) in
            
        }
        
    }
    
    
    @objc func changeStringBtnClick() {
        let contentStr : String = self.textView.attributedText.mgo_getPlainString()
        print("转换为普通文本::::"+"\(contentStr)")
        
    }
    
    
    func emojiKeyBoard(emojiKeyBoard: WQEmojiKeyBoard, emojiBtnClickForIndex: Int, value: String, img: String) {
        
//        var faceString : String = self.textView.text ?? ""
//        faceString.append(value)
//        let emotionTextAttachment = EmotionTextAttachment()
//        emotionTextAttachment.bounds = CGRect.init(x: 0, y: -5.5, width: 21, height: 21)
//        emotionTextAttachment.emotionStr = value
//        emotionTextAttachment.image = UIImage.init(named: img)
//        self.location = self.textView.selectedRange.location
//        let attri = NSAttributedString(attachment: emotionTextAttachment)
//        self.textView.textStorage.insert(attri, at: self.textView.selectedRange.location)
//        self.textView.textStorage.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.darkGray, range: NSRange.init(location: 0, length: self.textView.text.count))
//        self.textView.textStorage.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 16), range: NSRange.init(location: 0, length: self.textView.text.count))
//        self.textView.selectedRange = NSRange.init(location: self.location+1, length: 0)
        
    
    }
    
    
}

